package com.cjm.one.security.authenticationProvider;

import com.cjm.one.security.authenticationToken.EmailCodeAuthenticationToken;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

public class EmailCodeAuthenticationProvider implements AuthenticationProvider {
    

    private UserDetailsService appUserDetailsService;

    @Override
    public Authentication authenticate(Authentication authentication) {
        String email = (String) authentication.getPrincipal();
        // 这里已经在前置校验过验证码，直接加载用户
        UserDetails user = appUserDetailsService.loadUserByUsername(email);
        return new EmailCodeAuthenticationToken(user, null, user.getAuthorities());
    }


    @Override
    public boolean supports(Class<?> authentication) {
        return EmailCodeAuthenticationToken.class.isAssignableFrom(authentication);
    }
    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.appUserDetailsService = userDetailsService;
    }
}